import os
import json
import requests
import gitlab

root_dir = os.path.dirname(os.path.abspath(__file__))


class GitlabAPI(object):

    def __init__(self, *args, **kwargs):
        # self.gl = gitlab.Gitlab('https://xxxxxxxxxxx.com', private_token='xxxxxxxxxx',　　#参数为gitlab仓库地址和个人private_take
        #                    api_version='3')
        self.gl = args[0]

    def get_user_id(self, username):
        """
        通过用户名获取用户id
        :param username:
        :return:
        """
        user = self.gl.users.get_by_username(username)
        return user.id

    def get_group_id(self, groupname):
        """
        通过组名获取组id
        :param groupname:
        :return:
        """
        group = self.gl.groups.get(groupname, all=True)
        return group.id

    def get_user_projects(self, userid):
        """
        获取用户所拥有的项目
        :param userid:
        :return:
        """
        projects = self.gl.projects.owned(userid=userid, all=True)
        result_list = []
        for project in projects:
            result_list.append(project.http_url_to_repo)
        return result_list

    def get_group_projects(self, groupname):
        """
        获取组内项目！！！！！！！其他博客也有类似方法，实测不能拿到群组内项目，现经过小改动，亲测可满足要求
        :param groupname:
        :return:
        """
        group = self.gl.groups.get(groupname, all=True)
        projects = group.projects.list(all=True)
        return projects


    def getFileContent(self, projectID, file_path, branch):
        """
        通过项目id获取文件内容
        :param projectID:
        :return:
        """
        projects =self.gl.projects.get(projectID)
        f = projects.files.get(file_path=file_path, ref=branch)
        content = f.decode()
        # print(content)
        return content.decode('utf-8')

    def get_all_group(self):
        """
        获取所有群组
        :return:
        """
        return self.gl.groups.list(all=True)

if __name__ == '__main__':
    # 配置文件中的字符串不能加引号
    gl = gitlab.Gitlab.from_config('c7n', [os.path.join(root_dir, 'gitlabConf/python-gitlab.cfg')])
    # print(GitlabAPI(gl).getFileContent("124", "/customer-service-api/pom.xml", "master"))
    projects = gl.projects.list(all=True)

    for project in projects:
        print("%s,%s,%s" % (project.name, project.http_url_to_repo, project.description))

        

